//index.js
//获取应用实例
var app = getApp()
Page({
  data: {
  },
  onLoad:function(e){
    var that = this;
    if (e && e.enterfrom) {
      var enterfrom = e.enterfrom == 'share' ? true : false;
      that.setData({
        enterfrom: enterfrom
      })
    }
 
    if (!app.globalData.plumSession) {
      app.plumSessionCallback = plumSession => {
        that.requestCoupon();
      }
    } else {
      that.requestCoupon();
    }
  },
  requestCoupon: function () {
    var that = this;
    wx.showLoading({
      title: '加载中'
    })
    wx.request({
      url: app.globalData.requestUrl,
      data: {
        map: 'applet_coupon_list',
        type:'notIndex'
      },
      success: function (res) {
        console.log(res.data);
        if (res.data.ec == 200) {
          that.setData({
            couponList: res.data.data
          })
          that.djsTime();
        } else {
          that.setData({
            couponList: []
          })
        }
      },
      complete: function () {
        wx.hideLoading();
        wx.stopPullDownRefresh();
      }
    })
  },
  onPullDownRefresh:function(){
    var that = this;
    var couponList = that.data.couponList ? that.data.couponList : [];
    for (let j = 0; j < couponList.length; j++) {
      clearInterval(couponList[j].intervarID);
    }
    that.requestCoupon();
  },
  getCoupon: function (e) {
    var that = this;
    var curId = e.currentTarget.dataset.id;
    that.setData({
      curId: curId
    })
    app.getSubId(that, 'applet_coupon_receive', 'toGetCoupon');
  },
  // 领取优惠券跳转
  toGetCoupon: function (e) {
    var that = this;
    var id = that.data.curId;
    console.log(id);
    wx.showLoading({
      title: '领取中',
      mask: true
    })
    wx.request({
      url: app.globalData.requestUrl,
      data: {
        map: 'applet_coupon_receive',
        cid: id
      },
      success: function (res) {
        console.log(res.data);
        if (res.data.ec == 200) {
          var couponInfo = {
            couponDetail: res.data.data.coupon,
            receiveList: res.data.data.receiveList
          };
          wx.setStorage({
            key: "couponInfo",
            data: couponInfo,
            success: function () {
              wx.navigateTo({
                url: '/subpages/useCoupon/useCoupon'
              })
            }
          })

        } else {
          app.errorTip(that, res.data.em, 2000);
        }
      },
      complete: function () {
        wx.hideLoading();
      }
    })

  },
  toShareCoupon: function (e) {
    var id = e.target.dataset.id;
    if (!id) {
      return;
    }
    wx.navigateTo({
      url: '/subpages/shareCoupon/shareCoupon?id=' + id
    })
  },
  djsTime: function () {
    var that = this;
    var couponList = that.data.couponList;
    console.log(couponList);
    if (couponList.length > 0) {
      for (var i = 0; i < couponList.length; i++) {
        clearInterval(couponList[i].intervarID);
        that.djs(couponList[i].startLeft, couponList, i)
      }
    }
  },
  djs: function (time, couponList, i) {
    var that = this;
    var leftTime = time;  //剩余秒数
    console.log(leftTime);
    couponList[i].intervarID = setInterval(function () {
      leftTime--;
      that.formatTime(leftTime, couponList, i);
    }, 1000)
  },
  formatTime: function (leftTime, couponList, i) {
    var that = this;
    if (leftTime > 0) {
      var days = parseInt(leftTime / 60 / 60 / 24, 10);
      var hours = parseInt(leftTime / 60 / 60 % 24, 10);
      var minutes = parseInt(leftTime / 60 % 60, 10);
      var seconds = parseInt(leftTime % 60, 10);
      days = days < 10 ? '0' + days : days;
      hours = hours < 10 ? '0' + hours : hours;
      minutes = minutes < 10 ? '0' + minutes : minutes;
      seconds = seconds < 10 ? '0' + seconds : seconds;
      var remainTime = {
        days: days,
        hours: hours,
        minutes: minutes,
        seconds: seconds
      }
      couponList[i].remainTime = remainTime;
      that.setData({
        couponList: couponList
      })
      if (hours == '00' && minutes == '00' && seconds == '00') {
        clearInterval(couponList[i].intervarID);
      }
    } else {
      var remainTime = {
        days: '00',
        hours: '00',
        minutes: '00',
        seconds: '00'
      }
      couponList[i].remainTime = remainTime;
      couponList[i].startLeft = 0;
      that.setData({
        couponList: couponList
      })
    }
  },
  onShareAppMessage: function (e) {
    var that = this;
    var couponId = e.target.dataset.id;
    if (couponId) {
      var couponList = that.data.couponList;
      for (let i = 0; i < couponList.length; i++) {
        if (couponId == couponList[i].id) {
          couponList[i].needShare = 0;
        }
      }
      that.setData({
        couponList: couponList
      })
    }
    return {
      title: '领券大厅',
      imageUrl: '',
      path: '/subpages/couponList/couponList'
    }
  }
})
